package com.feidee.data.report.constant;

import org.apache.commons.lang.StringUtils;
import org.quartz.JobKey;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Constant {

	//环境变量标识
	public static String ENV = "live";

	// Controller
	public static final String RETURE_RSLT = "rslt";
	public static final String RETURE_CODE = "code";
	public static final String COST_TIME = "cost_time";
	public static final String SM = "ms";
	public static final String RUNNING = "db-query-running";
	public static final Integer RUNNING_Time = 60 * 60;
	public static final String FLAG = "flag";
	public static final String INFO = "sys-info";
	public static final String NOT_SUPT = "不支持该引擎查询";

	public static final String IMPALA_ENG = "Impala";
	public static final String HIVE_ENG = "Hive";
	public static final String MYSQL_ENG = "Mysql";
	public static final String SPARK_ENG = "Spark";
	public static final String SNAPPYDATA_ENG = "SnappyData";
	public static final String DRIVER_STR = "driverUrl";
	public static final String URL_STR = "urlStr";
	public static final String USERNAME_STR = "usernameStr";
	public static final String PASSWORD_STR = "passwordStr";

	public static final String SCRIT = "#feidee-data-key#";
	public static final String SCHEDULER_SCRIT = "#scheduler-feidee-data-key#";
	public static final String AUTH_INFO = "Auth info:";
	public static final String APT_KEY_ERROR = "Key或Apt有误";
	public static final String SELECT_PROFILE = "您只有SELECT权限";

	public static final String REFRESH_SPLIT = ",";


	public static final String MYSQL_HOST = "mysql.host";
	public static final String MYSQL_PORT = "mysql.port";
	public static final String MYSQL_USER = "mysql.warehouse.user";
	public static final String MYSQL_PASSWORD = "mysql.warehouse.pwd";
	public static final String MYSQL_DRIVER = "mysql.java.driver.class";
	public static final String MYSQL_DB = "mysql.db";
	public static final String MYSQL_BATCH_NUMBER = "batch.number";
	public static final String MYSQL_URL = "mysql.warehouse.url";
	public static final String MYSQL_JAVA_DRIVER_CLASS = "mysql.java.driver.class";

	public static final String MYSQL_DECRYPT_KEY = "c7c2H_7_dbHb8fMMMMec8d5mykey52d85bc6";
	public static final String MYSQL_BASE_INFO = "base_info";


	public static final String LOG_APT = "log.apt";
	public static final String LOG_BUS = "log.bus";
	public static final String FUNNEL_LOG_BUS = "funnel.log.bus";
	public static final String LOG_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String LOG_EXCEPTION = "Exception";
	public static final String LOG_START = "Start";
	public static final String LOG_SUCCESS = "Success";
	public static final String LOG_STOP = "Stop";

	public static final String REPORT_MAIL_EXCEPTION = "Exception";
	public static final String REPORT_MAIL_SUCCESS = "Success";

	public static final String MONITOR_LOG_EMAIL_TITLE = "数据平台日志收集异常报警";


	public static final String EMAIL_SERVER = "smtp.qq.com";
	public static final String EMAIL_USER = "979827772";
	public static final String EMAIL_PWD = "will634278GO";
	public static final String EMAIL_PORT = "25";
	public static final String EMAIL_FROM = "979827772@qq.com";
	public static final String[] EMAIL_TO = {"wei_quan@kingdee.com", "979827772@qq.com"};

	public static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
	public static final String HIVE_CONNECTION = "jdbc:hive2://192.168.241.113:10000/dw";
	public static final String HIVE_USER = "hive";
	public static final String HIVE_PWD = "hive";

	public static final String IMPALA_DRIVER = "com.cloudera.impala.jdbc41.Driver";
	public static String IMPALA_CONNECTION = "jdbc:impala://10.203.65.13:21050/default";
	public static final String IMPALA_USER = "data-report";
	public static final String IMPALA_PWD = "";

	public static String IMPALA_SUIYUN_CONNECTION = "jdbc:impala://10.202.97.28:21050/default";
	public static final String IMPALA_SUIYUN_USER = "data-report";
	public static final String IMPALA_SUIYUN_PWD = "";


	public static final String SPARK_SQL_CONNECTION = "jdbc:hive2://10.203.1.13:18000";
	public static final String REALREPORT_USER = "fdbd_rep";
	public static final String REALREPORT_PWD = "1vbr75zi";

	//public static final String SPARK_KUDU_CONNECTION = "jdbc:hive2://bj1-203-centos19:21050";
	public static final String SPARK_KUDU_CONNECTION = "jdbc:hive2://bj1-203-1-centos13:18012";
	public static final String SPARK_CONNECTION = "jdbc:hive2://10.203.1.13:10000/dw";
	public static final String SPARK_USER = "";
	public static final String SPARK_PWD = "";

	public static final String SNAPPY_DATA_DRIVER = "org.apache.hive.jdbc.HiveDriver";

	public static int THREAD_NUM = 10;    //一次执行使用线程数
	public static int QUEUE_SIZE = 500;   //存储sql队列大小，一组占用一条
	public static int MONITOR_THREAD_SLEEP_TIME = 59000;   //监控线程两次检测之间线程睡眠时间
	public static int EXEC_THREAD_POOL_SIZE = 2000;   //报表执行总线程池大小
	public static int FUNNEL_THREAD_POOL_SIZE = 2000;   //漏斗分析执行总线程池大小
	public static int REAL_TIME_THREAD_POOL_SIZE = 2000;   //实时报表执行总线程池大小
	public static int MONITOR_THREAD_POOL_SIZE = 200;   //报表监控总线程序池大小
	public static final String SQL_SPLIT = "€";

	public static final String ERR_MSG_SPLIT_FLAG = "€";// key分隔符
	public static String TASK_RES_RETURN_URI = "http://10.195.1.128:8111/bigdata/rest/resources/interfaces/apply/changeApp";
	public static List<JobKey> CurQuartzJobList = new ArrayList<JobKey>();
	public static final String FREQUENCY_SPLIT_FLAG = "#";
	public static final String ALL_FREQUENCY = "update.frequency";  //报表所有的更新频率列表(以'#'分割)
	public static final String CUSTOM_REPORT_MYSQL_DRIVER = "custom.mysql.driverClassName";
	public static final String CUSTOM_REPORT_MYSQL_URL = "custom.mysql.url";
	public static final String CUSTOM_REPORT_MYSQL_USER = "custom.mysql.username";
	public static final String CUSTOM_REPORT_MYSQL_PASSWD = "custom.mysql.password";
	public static final String CUSTOM_REPORT_MYSQL_TABLE_PREFIX = "tmp_";
	public static final String CUSTOM_VIEW_MYSQL_TABLE_PREFIX = "tmp_";
	public static final String CUSTOM_REPORT_MYSQL_TABLE_SELF = "temp_table";
	public static final String CUSTOM_REPORT_STATUS_SPLIT = "#";
	public static final String CUSTOM_REPORT_FLAG = "1";
	public static Map<String, String> CUSTOM_PRIORITY = new HashMap<>();

	//自定义报表状态值
	public static final String CR_STATUS_NO_CHANGE = "0";          //无改动，不需做任务操作
	public static final String CR_STATUS_NEW_REPORT = "1";         //新增报表，全部流程
	public static final String CR_STATUS_CHANGE_NEED_ALL_EXECUTE = "2";      //非新增报表，改动需要执行所有流程 [Mysql类型改变也会全部重跑，一般是结果写入Mysql出错才会更改类型]
	public static final String CR_STATUS_MYSQL_COLUMN_COMMENT_CHANGE = "3";  //Mysql注释改变
	public static final String CR_STATUS_REPORT_PEROID_CHANGE = "4";         //报表执行周期改变
	public static final String CR_STATUS_REPORT_COMMENT_CHANGE = "5";        //报表注释改变
	public static final String CR_STATUS_HIVESQL_CHANGE_HISTORYSQL_NOCHANGE = "6";  //实时sql改变,历史sql没改变
	public static final String CR_STATUS_HISTORYSQL_CHANGE_HIVESQL_NOCHANGE = "7";  //历史sql改变，实时sql没改变
	public static final String CR_STATUS_REPORT_DEPENDENCY_CHANGE = "8";  //报表依赖信息改变
	public static final String CR_STATUS_REPORT_PRIORITY_CHANGE = "9";         //报表优先级改变
	public static final String CR_STATUS_DATA_RESOURCES_CHANGE = "10";  //数据源改变

	//自定义报表发布状态
	public static final String CR_PUBSTATUS_NO_PUBLISH = "0";
	public static final String CR_PUBSTATUS_ALREADY_PUBLISH = "1";

	//报表依赖功能报表执行状态
	public static final String REPORT_DEPENDENCY_NO_EXECUTE = "WZX";         //未执行状态
	public static final String REPORT_DEPENDENCY_WAITTING_EXECUTE = "DDZX";  //等待执行
	public static final String REPORT_DEPENDENCY_EXECUTING = "ZZZX";         //正在执行
	public static final String REPORT_DEPENDENCY_EXECUTE_SUCCESS = "ZXCG";   //执行成功
	public static final String REPORT_DEPENDENCY_EXECUTE_FAILED = "ZXSB";    //执行失败
	public static final String REPORT_DEPENDENCY_WAIT_OVERTIME = "DDCS";     //报表在队列中等待执行超时
	public static final String REPORT_DATARESOURCES_WAIT_OVERTIME = "DRCS";     //报表在队列中等待源数据同步超时
	public static final String REPORT_DATARESOURCES_EXECUTE_FAILED = "DRSB";    //数据源同步失败导致执行失败

	//报表依赖字符串分隔符
	public static final String REPORT_DEPENDENCY_SPLIT_FLAG = ",";
	//报表依赖功能：如果依赖未完成，该线程将执行任务返回后的睡眠时间[默认为3分钟]
	public static long EXECUTE_REPORT_THREAD_SLEEP_TIME = 3 * 60 * 1000L;
	//报表依赖功能：如果依赖未完成，改条报表执行等待的最大时长[默认为4小时]
	public static long REPORT_WAIT_MAX_TIME = 4 * 60 * 60 * 1000L;

	//报表负责人信息获取接口
	public static String EMAIL_GET_RECEIVER_URL = "http://10.195.1.151:7788/bigdata/rest/resources/interfaces/apply/tableCreaterInfo";

	public static String CURRENT_ALIVE_HOST_IP = "";
	public static String CURRENT_ALIVE_HOST_IP_KEY = "CURRENT_ALIVE_HOST_IP";

	//UUID前缀,根据主机Host信息生成
	public static String UUID_IP_PREFIX = "";

	// clickhouse服务器个数
	public static int CLICKHOUSE_NUM = 13;
	// clickhouse查询等待时间
	public static final long CLICKHOUSE_WAIT_TIME = 30;
	// 漏斗查询需要替换成md5查询的字段
	public static Set<String> FUNNEL_REPLACE_FIELDS;

	// 实时报表查询等待时间
	public static final long REALTIME_WAIT_TIME = 60;
	public static Set<String> SNAPPYDATA_RUN_ON_36 = new HashSet<>();

	public static HashMap<String, Set<String>> PRINT_RESULT_SQL = new HashMap<>();

	// 数据源校验要忽略掉的库
	public static Set<String> CONTAIN_WAREHOUSE;
	// 数据源校验要忽略掉的库
	public static Set<String> FILTER_TABLE;

	// 存储spark引擎执行结果为0条的SQL记录
	public static Map<String, List<String>> MONITOR_SQL = new HashMap<String, List<String>>() {
		@Override
		public String toString() {
			StringBuilder stringBuilder = new StringBuilder("\n报表名\tSQL下标\n");
			Set<Entry<String, List<String>>> entries = this.entrySet();
			Iterator<Entry<String, List<String>>> iterator = entries.iterator();
			while (iterator.hasNext()) {
				Entry<String, List<String>> next = iterator.next();
				String key = next.getKey();
				List<String> value = next.getValue();
				String join = StringUtils.join(value, ",");
				stringBuilder.append(key).append("\t").append(join).append("\n");
			}
			return stringBuilder.toString();
		}
	};

	public static Map<Integer, Map<String, String>> SPARK_THRIFT = new HashMap<>();
	public static Map<Integer, Set<String>> RUNNING_SQL = new ConcurrentHashMap<>();
	public static long SPARK_THRIFT_OVERTIME;
	public static long HIVE_RUN_OVERTIME;
	// 允许超时运行的SQL
	public static HashMap<String, Set<String>> ALLOW_OVERTIME_SQL = new HashMap<>();
}
